package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping
    public Result getUsers(UserDTO userDto) {
        PageResult<UserDO> userPage = userService.getUsers(userDto);
        return Result.success(userPage);
    }

    @PostMapping
    public Result addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable("ids") List<Integer> ids) {
        userService.deleteUser(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @PutMapping
    public Result updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){return Result.success(userService.list());}

    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        return Result.success(userService.listByRole(roleLabel));
    }
}
